﻿<link href="@Href("~/Modules/NineSeas/Styles/authoring.books.css")" rel="stylesheet" type="text/css"/>
<link href="@Href("~/Modules/NineSeas/Styles/books.summary.css")" rel="stylesheet" type="text/css"/>
<div class="books-container">   
    <div class="clear: both;"></div>
    @if (Model.Books != null && Model.Books.Count > 0) {
        <ul class="contentItems">
        @foreach (var book in Model.Books) {
            var imageUrl = String.IsNullOrWhiteSpace(book.ImageUrl) ?
                Href("~/Modules/NineSeas/Images/placeholder-book.png")
                : book.ImageUrl;
            var tagsHtml = new List<IHtmlString>();
            foreach (var t in book.Tags)
            {
                if (tagsHtml.Any())
                {
                    tagsHtml.Add(new HtmlString(" "));
                }
                tagsHtml.Add(Html.ActionLink((string)t.TagName, "Search", "Home", new { area = "Orchard.Tags", tagName = (string)t.TagName }, new { }));
            }
            <li>
                <div class="book-leftPane">
                    <a href="@Url.Action("Index", "BookView", new {name = book.NameOnUrl})">
                        <img src="@imageUrl" alt="@book.Name" class="book-image"/>
                    </a>
                </div>
                <div class="book-rightPane">
                    <div class="book-rightPane-header">                           
                        <div class="book-commands">
                            <a href="@Url.Action("Edit", "Books", new {book.Id})" title="@T("Edit")">@T("Edit")</a>@T(" | ")
                            <a href="@Href(String.Format("~/Authoring/Books/Book/{0}/Chapters", book.Id))" title="@T("Chapters")">@book.ChapterCount @T("Chapters")</a>
                            @T(" | ")
                            <a href="@Url.Action("Delete", "Books", new { book.Id, returnUrl = Request.Url.PathAndQuery })">@T("Delete")</a>
                        </div>
                    </div>
                    <div class="book-rightPane-content">      
                        <div class="book-name">
                            <a href="@Url.Action("Index", "BookView", new { name = book.NameOnUrl })">@book.Name</a>
                            @if (!book.VersionRecord.Published)
                            {
                                var message = book.DraftCount > 0 ? T("Book has drafts - {0} modified chapter(s) are unpublished", book.DraftCount) : T("Book has draft.");
                                @Display(New.Controls_HelpPopup(Message: message, Width: 180))
                            }
                        </div>
                        <div class="book-author" data-id="@book.AuthorId"></div>
                        <div class="book-createdDate">@T("Created") @book.CreatedDate</div>
                        <div class="book-lastUpdatedDate">@T("Last Updated") @book.LastUpdatedDate</div>
                        <div class="book-introduction">
                            @Html.Excerpt((string)book.Introduction, 300)
                        </div>
                        <div class="tags">@foreach(var htmlString in tagsHtml) { @htmlString }</div>  
                    </div>
                    <div class="book-rightPane-footer"></div>
                </div>
            </li>
        } 
        </ul>
        <script type="text/javascript">
            $(document).ready(function () {
                var authorIds = [];
                $('.book-author').each(function () {
                    if ($(this).attr("data-id") != ''
                        && $(this).attr("data-id") != null
                        && authorIds.indexOf($(this).attr("data-id")) == -1) {
                        authorIds.push($(this).attr("data-id"));
                    }
                });

                $.each(authorIds, function (index, item) {
                    $.ajax({
                        url: '@Href("~/api/nineseas/authors/")' + item,
                        cache: false,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        complete: function (data) {
                            var author = $.parseJSON(data.responseText);

                            var authorLink = '<a href="@Href("~/")Authors/Author/' + item + '/Details">' + author.DisplayName + '</a>';
                            $('.book-author[data-id=' + item + ']').html(authorLink);
                        }
                    });
                });
            });
        </script>
    } else {
        <b><i>@T("You haven't posted any book. Please use the button on the bottom to create a new one.")</i></b>
    }
</div>
@{
    Func<dynamic, dynamic> linkCreator = i => Url.Action("Index", "Books", new {
        @Expression = Model.Search.Expression,
        @currentPage = i,
        @pageSize = Model.Pager.PageSize
    });
}
<div class="books-paging">    
    @Display(New.Books_Pager(
        Paging: Model.Paging,
        LinkCreator: linkCreator
    ))
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $.commandBar('#nineseas-reader-footer', {
            resetElement: true,
            left: [],
            right: [$(".books-paging:last-of-type"), {
                id: 'createButton',
                type: 'a', // button | a
                href: '@Url.Action("Edit", "Books")',
                        caption: '@T("Create New Book")'
            }]
        });
        var pagingOpening = false;
        var panel = $(".books-paging ul");
        $(".books-paging .pager-current, .books-paging .total-pages").on("click", function () {
            var newState = panel.css('display') == 'none' ? 'block' : 'none';

            pagingOpening = newState == 'block';
            if (pagingOpening) {
                panel.slideToggle('slow');
            }
        });

        $(".books-paging ul a").on("click", function (e) {
            var $this = $(this);
            e.preventDefault();

            $.get($this.attr("href"), function (data) {
                $("#nineseas-reader-footer .books-paging").remove();

                var activeTabIndex = $("#authoring-tabs").tabs("option", "active") + 1;
                $(".ui-tabs-panel:nth-of-type(" + activeTabIndex + ")").html(data);
            });
        });

        $(document).click(function () {
            if (!pagingOpening) {
                if (panel.css('display') != 'none') {
                    panel.slideToggle('slow');
                }
            }
            pagingOpening = false;
        });
    });
</script>